using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases{

public class CLSDETORDP:ObservableObject{

#region Metodos basicos tabla

MDOgeneral mod = new MDOgeneral();

//Consultar cantidad ordenada por talla
public DataTable conCanOrdTal() {
    try {
        string query = "select codtal_pro,codtal_dop,canord_dop from (select codtal_pro,codref_pro,codcol_pro from maepro where (codref_pro||codcol_pro)=(select distinct(codref_dop||codcol_dop) from detordp where nroord_dop='"+NROORD_DOP+"')) left outer join detordp on codref_pro=codref_dop and codcol_pro=codcol_dop and codtal_pro=codtal_dop and nroord_dop='"+NROORD_DOP+"'  order by codtal_pro";
        return mod.consulta(query);
    }
    catch (Exception ex) { throw ex; }
}

//Consultar cantidad ordenada de una orden
public void conCanOrd() {
    try{
    
       string query = "select  sum(canord_dop) as canord_dop from detordp where nroord_dop='" + NROORD_DOP + "'";
       DataTable val=  mod.consulta(query);
       if (val.Rows.Count > 0)
       {
           CANORD_DOP = Convert.ToInt32(val.Rows[0]["canord_dop"]);
       }
       else {
           CANORD_DOP = 0;
       }

    }
    catch (Exception ex) { throw ex; }
}

//Consultar tallas de una orden
public DataTable consTallas() {
    try {
        string query = "select Distinct(TRIM(CODTAL_DOP)) as CODTAL_DOP from detordp where NROORD_DOP='" + NROORD_DOP + "' order by  CODTAL_DOP";
    return mod.consulta(query);
    }
    catch (Exception ex) { throw ex; }
}

public void consultar()
{
    try
    {
        string query = "select NROORD_DOP,CODREF_DOP,CODCOL_DOP,CODTAL_DOP,CANORD_DOP,CANCOR_DOP,CANGUA_DOP,CANMON_DOP,CANDES_DOP,CANPRP_DOP,CANPRG_DOP from DETORDP where NROORD_DOP='"+NROORD_DOP+"' and CODREF_DOP='"+CODREF_DOP+"' and CODCOL_DOP='"+CODCOL_DOP+"' and CODTAL_DOP='"+CODTAL_DOP+"'";
        DataTable val = mod.consulta(query);
        if (val.Rows.Count > 0)
        {
            NROORD_DOP = val.Rows[0]["NROORD_DOP"].ToString();
            CODREF_DOP = val.Rows[0]["CODREF_DOP"].ToString();
            CODCOL_DOP = val.Rows[0]["CODCOL_DOP"].ToString();
            CODTAL_DOP = val.Rows[0]["CODTAL_DOP"].ToString();
            CANORD_DOP = Convert.ToInt32(val.Rows[0]["CANORD_DOP"]);
            CANCOR_DOP = Convert.ToInt32(val.Rows[0]["CANCOR_DOP"]);
            CANGUA_DOP = Convert.ToInt32(val.Rows[0]["CANGUA_DOP"]);
            CANMON_DOP = Convert.ToInt32(val.Rows[0]["CANMON_DOP"]);
            CANDES_DOP = Convert.ToInt32(val.Rows[0]["CANDES_DOP"]);
            CANPRP_DOP = Convert.ToInt32(val.Rows[0]["CANPRP_DOP"]);
            CANPRG_DOP = Convert.ToInt32(val.Rows[0]["CANPRG_DOP"]);
            EXISTE = true;
            CAMBIO = false;
        }
        else
        {
            EXISTE = false;
        }

    }
    catch (Exception e) { throw e; }
}

public int eliminar(){
  string query="delete from DETORDP where (NROORD_DOP='"+NROORD_DOP+"') and (CODREF_DOP='"+CODREF_DOP+"') and (CODCOL_DOP='"+CODCOL_DOP+"') and (CODTAL_DOP='"+CODTAL_DOP+"')";
  return mod.comando(query);
  }

public int actualizar(){
  string query="update DETORDP set CANORD_DOP="+CANORD_DOP+",CANCOR_DOP="+CANCOR_DOP+",CANGUA_DOP="+CANGUA_DOP+",CANMON_DOP="+CANMON_DOP+",CANDES_DOP="+CANDES_DOP+",CANPRP_DOP="+CANPRP_DOP+",CANPRG_DOP="+CANPRG_DOP+" where (NROORD_DOP='"+NROORD_DOP+"') and (CODREF_DOP='"+CODREF_DOP+"') and (CODCOL_DOP='"+CODCOL_DOP+"') and (CODTAL_DOP='"+CODTAL_DOP+"')";
  return mod.comando(query);
  }

public int insertar(){

    try
    {                                                                                                                                                                                     
        string query = "insert into DETORDP (NROORD_DOP,CODREF_DOP,CODCOL_DOP,CODTAL_DOP,CANORD_DOP,CANCOR_DOP,CANGUA_DOP,CANMON_DOP,CANDES_DOP,CANPRP_DOP,CANPRG_DOP) values ('"+NROORD_DOP+"','"+CODREF_DOP+"','"+CODCOL_DOP+"','"+CODTAL_DOP+"',"+CANORD_DOP+","+CANCOR_DOP+","+CANGUA_DOP+","+CANMON_DOP+","+CANDES_DOP+","+CANPRP_DOP+","+CANPRG_DOP +")";
        return mod.comando(query);

    }
    catch (Exception ex) { throw (ex); }
  }

public Boolean existe() 
{
  string query="select  coalesce(count(NROORD_DOP),0) from DETORDP where (NROORD_DOP='"+NROORD_DOP+"') and (CODREF_DOP='"+CODREF_DOP+"') and (CODCOL_DOP='"+CODCOL_DOP+"') and (CODTAL_DOP='"+CODTAL_DOP.Replace("'","''") +"')";
  DataTable val = mod.consulta(query);
  if (val.Rows[0][0].ToString().Equals("0")){
  return false;
  }
  return true;
}

public int consCant()
{
    string query = "select CANORD_DOP from DETORDP where (NROORD_DOP='" + NROORD_DOP + "') and (CODREF_DOP='" + CODREF_DOP + "') and (CODCOL_DOP='" + CODCOL_DOP + "') and (CODTAL_DOP='" + CODTAL_DOP + "')";
    DataTable val = mod.consulta(query);
    return Convert.ToInt32(val.Rows[0][0]);
}

public double CantidadOrdenada(string Ord, string Ref, string Col, string Tal)
{
    try
    {
        string query = "select canord_dop from detordp where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        DataTable dt= mod.consulta(query);
        if (dt.Rows.Count > 0) return System.Convert.ToDouble(dt.Rows[0][0].ToString());
        return 0;
    }
    catch (Exception ex) { throw (ex); }
}

public double CantidadCortada(string Ord, string Ref, string Col, string Tal)
{
    try
    {
        string query = "select cancor_dop from detordp where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        DataTable dt = mod.consulta(query);
        if (dt.Rows.Count > 0) return System.Convert.ToDouble(dt.Rows[0][0].ToString());
        return 0;
    }
    catch (Exception ex) { throw (ex); }
}

public double CantidadGuarnecida(string Ord, string Ref, string Col, string Tal)
{
    try
    {
        string query = "select cangua_dop from detordp where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        DataTable dt = mod.consulta(query);
        if (dt.Rows.Count>0) return System.Convert.ToDouble(dt.Rows[0][0].ToString());
        return 0;
    }
    catch (Exception ex) { throw (ex); }
}

public double CantidadMontaje(string Ord, string Ref, string Col, string Tal)
{
    try
    {
        string query = "select canmon_dop from detordp where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        DataTable dt = mod.consulta(query);
        if (dt.Rows.Count > 0) return System.Convert.ToDouble(dt.Rows[0][0].ToString());
        return 0;
    }
    catch (Exception ex) { throw (ex); }
}

public double CantidadDespachada(string Ord, string Ref, string Col, string Tal)
{
    try
    {
        string query = "select candes_dop from detordp where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        DataTable dt = mod.consulta(query);
        if (dt.Rows.Count > 0) return System.Convert.ToDouble(dt.Rows[0][0].ToString());
        return 0;
    }
    catch (Exception ex) { throw (ex); }
}

public double CantidadPreparada(string Ord, string Ref, string Col, string Tal)
{
    try
    {
        string query = "select canprp_dop from detordp where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        DataTable dt = mod.consulta(query);
        if (dt.Rows.Count > 0) return System.Convert.ToDouble(dt.Rows[0][0].ToString());
        return 0;
    }
    catch (Exception ex) { throw (ex); }
}

public double CantidadProgramada(string Ord, string Ref, string Col, string Tal)
{
    try
    {
        string query = "select canprg_dop from detordp where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        DataTable dt = mod.consulta(query);
        if (dt.Rows.Count > 0) return System.Convert.ToDouble(dt.Rows[0][0].ToString());
        return 0;
    }
    catch (Exception ex) { throw (ex); }
}

public int ActCanPrg(string Ord, string Ref, string Col, string Tal, double Can)
{
    try
    {
        string query = "update detordp set canprg_dop=canprg_dop + " + Can + " where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        return mod.comando(query);
    }
    catch (Exception ex) { throw (ex); }
}

public int ActCanDes(string Ord, string Ref, string Col, string Tal, double Can)
{
    try
    {
        string query = "update detordp set candes_dop=candes_dop + " + Can + " where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        return mod.comando(query);
    }
    catch (Exception ex) { throw (ex); }
}

public int ActCanCor(string Ord, string Ref, string Col, string Tal, double Can)
{
    try
    {
        string query = "update detordp set cancor_dop=cancor_dop + " + Can + " where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        return mod.comando(query);
    }
    catch (Exception ex) { throw (ex); }
}

public int ActCanPrp(string Ord, string Ref, string Col, string Tal, double Can)
{
    try
    {
        string query = "update detordp set canprp_dop=canprp_dop + " + Can + " where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        return mod.comando(query);
    }
    catch (Exception ex) { throw (ex); }
}

public int ActCanGua(string Ord, string Ref, string Col, string Tal, double Can)
{
    try
    {
        string query = "update detordp set cangua_dop=cangua_dop + " + Can + " where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        return mod.comando(query);
    }
    catch (Exception ex) { throw (ex); }
}

public int ActCanMon(string Ord, string Ref, string Col, string Tal, double Can)
{
    try
    {
        string query = "update detordp set canmon_dop=canmon_dop + " + Can + " where nroord_dop='" + Ord + "' and codref_dop='" + Ref + "' and codcol_dop='" + Col + "' and Codtal_dop='" + Tal + "'";
        return mod.comando(query);
    }
    catch (Exception ex) { throw (ex); }
}

#endregion

#region  Propiedades tabla DETORDP

    private string _NROORD_DOP;
    public string NROORD_DOP{
        get { return _NROORD_DOP;}
        set { _NROORD_DOP=value;
        RaisePropertyChangedEvent("NROORD_DOP");
        }
    }
    private string _CODREF_DOP;
    public string CODREF_DOP{
        get { return _CODREF_DOP;}
        set { _CODREF_DOP=value;
        RaisePropertyChangedEvent("CODREF_DOP");
        }
    }
    private string _CODCOL_DOP;
    public string CODCOL_DOP{
        get { return _CODCOL_DOP;}
        set { _CODCOL_DOP=value;
        RaisePropertyChangedEvent("CODCOL_DOP");
        }
    }
    private string _CODTAL_DOP;
    public string CODTAL_DOP{
        get { return _CODTAL_DOP;}
        set { _CODTAL_DOP=value;
        RaisePropertyChangedEvent("CODTAL_DOP");
        }
    }
    private int _CANORD_DOP;
    public int CANORD_DOP{
        get { return _CANORD_DOP;}
        set {
            if (_CANORD_DOP != value) { CAMBIO = true; } 
            _CANORD_DOP=value;
            RaisePropertyChangedEvent("CANORD_DOP");
        }
    }
    private int _CANCOR_DOP;
    public int CANCOR_DOP{
        get { return _CANCOR_DOP;}
        set {
            if (_CANCOR_DOP != value) { CAMBIO = true; } 
            _CANCOR_DOP=value;
            RaisePropertyChangedEvent("CANCOR_DOP");
        }
    }
    private int _CANGUA_DOP;
    public int CANGUA_DOP{
        get { return _CANGUA_DOP;}
        set {
            if (_CANGUA_DOP != value) { CAMBIO = true; } 
            _CANGUA_DOP=value;
            RaisePropertyChangedEvent("CANGUA_DOP");
        }
    }
    private int _CANMON_DOP;
    public int CANMON_DOP{
        get { return _CANMON_DOP;}
        set {
            if (_CANMON_DOP != value) { CAMBIO = true; } 
            _CANMON_DOP=value;
            RaisePropertyChangedEvent("CANMON_DOP");
        }
    }
    private int _CANDES_DOP;
    public int CANDES_DOP{
        get { return _CANDES_DOP;}
        set {
            if (_CANDES_DOP != value) { CAMBIO = true; } 
            _CANDES_DOP=value;
            RaisePropertyChangedEvent("CANDES_DOP");
        }
    }
    private int _CANPRP_DOP;
    public int CANPRP_DOP{
        get { return _CANPRP_DOP;}
        set {
            if (_CANPRP_DOP != value) { CAMBIO = true; } 
            _CANPRP_DOP=value;
            RaisePropertyChangedEvent("CANPRP_DOP");
        }
    }
    private int _CANPRG_DOP;
    public int CANPRG_DOP{
        get { return _CANPRG_DOP;}
        set {
            if (_CANPRG_DOP != value) { CAMBIO = true; } 
            _CANPRG_DOP=value;
            RaisePropertyChangedEvent("CANPRG_DOP");
        }
    }
    private Boolean _CAMBIO = false;
    public Boolean CAMBIO
    {
        get { return _CAMBIO; }
        set
        {
            _CAMBIO = value;
            RaisePropertyChangedEvent("CAMBIO");
        }
    }
    private Boolean _EXISTE = false;
    public Boolean EXISTE
    {
        get { return _EXISTE; }
        set
        {
            _EXISTE = value;
            RaisePropertyChangedEvent("EXISTE");
        }
    }

#endregion

   }
}
